﻿using DelonixRegia.Classes;
using DelonixRegia.Classes.Rooms;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DelonixRegia
{
    public partial class RoomAvailability : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxNoGuest.Text))
            {
                SqlCommand comm = new SqlCommand();
                comm.CommandText = "SELECT * FROM Room WHERE (roomNo NOT IN (SELECT roomNo FROM RoomBooking WHERE bookingID NOT IN (SELECT bookingID FROM RoomCheckout))) AND capacity >= @capacity";
                comm.Parameters.AddWithValue("@capacity", tbxNoGuest.Text);

                if (!ddlRoomType.SelectedItem.Text.Equals("Not Selected"))
                {
                    comm.CommandText += " AND roomType=@roomType";
                    comm.Parameters.AddWithValue("@roomType", ddlRoomType.SelectedItem.Value);
                }

                if (rblSmoke.SelectedItem != null)
                {
                    comm.CommandText += " AND smoking=@smoking";
                    int smoke = 0;
                    if (rblSmoke.SelectedItem.Text.Equals("Smoking")) smoke = 1;
                    else if (rblSmoke.SelectedItem.Text.Equals("Non-Smoking")) smoke = 0;
                    comm.Parameters.AddWithValue("@smoking", smoke);
                }

                if (rblBalcony.SelectedItem != null)
                {
                    comm.CommandText += " AND balcony=@balcony";
                    int balcony = 0;
                    if (rblBalcony.SelectedItem.Text.Equals("Yes")) balcony = 1;
                    else if (rblBalcony.SelectedItem.Text.Equals("No")) balcony = 0;
                    comm.Parameters.AddWithValue("@balcony", balcony);
                }

                if (rblLevel.SelectedItem != null)
                {
                    if (rblLevel.SelectedItem.Text.Equals("High Level"))
                    {
                        comm.CommandText += " AND roomLevel > 2";
                    }
                    else if (rblLevel.SelectedItem.Text.Equals("Low Level"))
                    {
                        comm.CommandText += " AND roomLevel <= 2";
                    }
                }

                IEnumerable<Room> availableRooms = RoomDBManager.getAvailableRoom(comm);
                ArrayList list = new ArrayList();

                foreach (Room r in availableRooms)
                {
                    ViewRoom vr = new ViewRoom();
                    vr.RoomNumber = r.roomNo;
                    vr.RoomLevel = r.roomLevel;
                    vr.RoomType = RoomDBManager.getRoomType(r.roomType).name;
                    vr.RoomRate = r.roomRate;
                    list.Add(vr);
                }

                gvResult.DataSource = list.Cast<ViewRoom>();
                gvResult.DataBind();
            }
            else if (!string.IsNullOrEmpty(tbxRoomNo.Text))
            {
                ArrayList list = new ArrayList();
                Room r = RoomDBManager.getRoomByRoomNo(int.Parse(tbxRoomNo.Text));

                if (r != null)
                {
                    ViewRoom vr = new ViewRoom();

                    vr.RoomNumber = r.roomNo;
                    vr.RoomLevel = r.roomLevel;
                    vr.RoomType = RoomDBManager.getRoomType(r.roomType).name;
                    vr.RoomRate = r.roomRate;

                    list.Add(vr);
                }

                gvResult.DataSource = list;
                gvResult.DataBind();
            }
            else
                ClientScriptMgr.showErrorBox("You need to enter either the <i>number of guest</i> or <i>room number</i>!");

            pnlFirstStep.Visible = false;
            pnlSecondStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("RoomAvailability.aspx");
        }

        protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[1].Text = "Room Number";
                e.Row.Cells[2].Text = "Level";
                e.Row.Cells[3].Text = "Room Type";
                e.Row.Cells[4].Text = "Room Rate";
            }
        }
    }
}